package mi.payment.constants;

import java.util.HashMap;
import java.util.Map;

import mi.payment.bean.db.CardInfo;
import mi.payment.bean.db.ICCardInfo;
import mi.payment.bean.db.MAGCardInfo;
import mi.payment.bean.db.TradeData;
import mi.payment.db.dao.PayDetail;
import mi.payment.db.dao.UserInfo;
import mi.payment.settings.MerManagerSettings;
import mi.payment.settings.MerchantSettingsOperation;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.settings.SystemParamsSettings;
import mi.payment.utils.SystemDateTime;
import sunmi.payment.L3.L3Params;

/**
 * @author tomcat on 2017/1/8.
 */
public class Params {

    private static final String TAG = "Params";

    // 保存初始密钥的key
    public static final String START_KEY = "SAVE_START_KEY";

    public static Map<String, Object> CONFIG_PARAMS = new HashMap<>();

    // 6900 入网许可证编号 签到的62域需要用到
    public static String NetworkAccessLicenseNo = "6900";

    // 当前登录的用户信息
    public static UserInfo userInfo;

    // 全局参数变量
    private static TradeData sTradeData;

    public static L3Params sL3Params;

    public static L3Params initL3Params() {
        sL3Params = new L3Params();
        return sL3Params;
    }

    public static TradeData newTradeData() {
        return initTradeData();
    }

    public static TradeData getTradeData() {
        // fix #1284 java.lang.NullPointerException -2017-10-31
        // java.lang.NullPointerException: Attempt to invoke virtual method 'mi.payment.db.dao.PayDetail mi.payment.bean.db.TradeData.getPayDetail()' on a null object reference
        if (sTradeData == null) {
            newTradeData();
        }
        return sTradeData;
    }

    private static TradeData initTradeData() {
        TradeData tradeData = new TradeData();
        PayDetail payDetail = new PayDetail();
        CardInfo cardInfo = new CardInfo();
        MAGCardInfo magCardInfo = new MAGCardInfo();
        cardInfo.setMAGCardInfo(magCardInfo);
        ICCardInfo icCardInfo = new ICCardInfo();
        cardInfo.setICCardInfo(icCardInfo);
        tradeData.setPayDetail(payDetail);
        tradeData.setCardInfo(cardInfo);

        SystemParamsOperation dataOperation = SystemParamsOperation.getInstance();
        SystemParamsSettings systemParamsSettings = dataOperation.getSystemParamsSettings();
        MerchantSettingsOperation merchantSettingsOperation = MerchantSettingsOperation.getInstance();
        MerManagerSettings merManagerSettings = merchantSettingsOperation.getMerManagerSettings();

        if (Params.userInfo != null) {
            payDetail.operatorNo = Params.userInfo.getUId();
        }
        payDetail.BathNo = systemParamsSettings.getBatchNumStart();
        payDetail.voucherNo = dataOperation.getIncrementSerialNum();
        payDetail.terminalNo = merManagerSettings.getTerminalNum();
        payDetail.merchantNo = merManagerSettings.getMerchantNum();
        payDetail.merchantName = merManagerSettings.getMerchantName();
        payDetail.setCurrencyCode("156");
        payDetail.transNum = System.currentTimeMillis() / 1000;

        String strDate = SystemDateTime.getMMDD();
        String strTime = SystemDateTime.getHHmmss();
        payDetail.setTradeDate(strDate);
        payDetail.setTradeTime(strTime);

        sTradeData = tradeData;
        return sTradeData;
    }

    public static void resetTradeData() {
        sTradeData = null;
    }

}
